<?php
file_exists('test') or mkdir('test');

$test=fopen('test2.txt','w');
fwrite($test,123);
  copy('test2.txt','test/test2.txt');
  unlink('test2.txt');

$count=0;
test($count);
echo $count;
if($count>=3){
    $content=test($count,1);
    echo $content;
    file_put_contents('test/test.txt',$content);
}
function test(&$count,$num='',$content=''){
    $arr=scandir('test');
    foreach ($arr as $value){
        $txt=substr($value,-3);
        if($txt=='txt' && $num==''){
            $count++;
        }elseif($txt=='txt' && $num==1){
            echo 'test/'.$value;
            $content.=file_get_contents('test/'.$value);
            unlink('test/'.$value);
        }
    }
    if($num==1){
        return $content;
    }
}